package com.situ.science.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.situ.science.listener.ProjectExcelListener;
import com.situ.science.mapper.ProjectMapper;
import com.situ.science.pojo.Project;
import com.situ.science.pojo.query.ProjectQuery;
import com.situ.science.pojo.vo.ProjectExcelVo;
import com.situ.science.pojo.vo.ProjectVo;
import com.situ.science.pojo.vo.ResearchersVo;
import com.situ.science.service.IProjectService;
import com.situ.science.utils.ExcelUtil;
import com.situ.science.utils.PageRusults;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectServiceImpl implements IProjectService {
    @Autowired
    ProjectMapper projectMapper;
    @Override
    public PageRusults list(ProjectQuery projectQuery) {
        int limit = projectQuery.getLimit()!= null? projectQuery.getLimit() : 10; // 设置默认值为 10，如果 limit 为 null
        int page = projectQuery.getPage()!= null? projectQuery.getPage() : 1;

        PageHelper.startPage(page, limit);
        //1.查询当前页列表
        List<ProjectVo> projectVoList = projectMapper.list(projectQuery);
        //2.用一个小插件查询总数量
        PageInfo pageInfo = new PageInfo(projectVoList);
        int total = (int) pageInfo.getTotal();
        return new PageRusults(0,"",total,projectVoList);
    }

    @Override
    public void update(ProjectVo projectVo) {
        projectMapper.update(projectVo);
    }

    @Override
    public ProjectVo selectById(Integer id) {
        ProjectVo projectVo = projectMapper.selectById(id);
        return projectVo;
    }


    @Override
    public List<ProjectVo> selectByRes(Integer id) {

        //1.查询当前页列表
        List<ProjectVo> projectVoList = projectMapper.selectByRes(id);

        return projectVoList;
    }

    @Override
    public void add(ProjectVo projectVo) {
        projectMapper.add(projectVo);
    }

    @Override
    public void deleteOne(Integer id) {
        projectMapper.deleteOne(id);
    }

    @Override
    public void deleteAll(List<Integer> ids) {
        projectMapper.deleteAll(ids);
    }

    @Override
    public void addByRes(ResearchersVo researchersVo,Integer id) {
        projectMapper.addByRes(researchersVo,id);
    }

    @Override
    public void updateStatus(ProjectVo projectVo) {
        System.out.println("service中projectVo:"+projectVo);
        projectMapper.updateStatus(projectVo);
    }

    @Override
    public void exportExcel(HttpServletResponse resp) {
        List<ProjectVo> projectVoList = projectMapper.selectAll();
        System.out.println("projectVoList"+projectVoList);
        List<ProjectExcelVo> projectExcelVoList = new ArrayList<>();
        for (ProjectVo projectVo : projectVoList) {
            ProjectExcelVo projectExcelVo = new ProjectExcelVo();
            BeanUtils.copyProperties(projectVo, projectExcelVo);
            System.out.println("projectExcelVo"+projectExcelVo);
            projectExcelVoList.add(projectExcelVo);
        }
        System.out.println("dada"+projectExcelVoList);
        ExcelUtil.exportExcel(resp,projectExcelVoList,ProjectExcelVo.class,"ProjectVo");
    }

    @Override
    public void importExcel(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), ProjectExcelVo.class,new ProjectExcelListener(projectMapper)).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Project> selectByContestId(Integer contestId) {
        return List.of();
    }

}
